local tuilang = fk.CreateSkill{
  name = "lingling__tuilang",
}

Fk:loadTranslationTable{
  ["lingling__tuilang"] = "推浪",
  [":lingling__tuilang"] = "当你失去过的总黑色牌数变为三的倍数时，你可以摸两张牌，然后弃置任意张牌。",

  ["@lingling__tuilang"] = "推浪",
  ["#lingling__tuilang-discard"] = "推浪：你可以弃置任意张牌",
}

tuilang:addEffect(fk.AfterCardsMove, {
  anim_type = "drawcard",
  can_trigger = function (self, event, target, player, data)
    if player:hasSkill(tuilang.name) then
      for _, move in ipairs(data) do
        if move.extra_data and move.extra_data.lingling__tuilang and table.contains(move.extra_data.lingling__tuilang, player.id) then
          return true
        end
      end
    end
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    player:drawCards(2, tuilang.name)
    if not player.dead and not player:isNude() then
      room:askToDiscard(player, {
        min_num = 1,
        max_num = 999,
        include_equip = true,
        skill_name = tuilang.name,
        cancelable = true,
        prompt =  "#lingling__tuilang-discard",
      })
    end
  end,

  can_refresh = function (self, event, target, player, data)
    if player:hasSkill(tuilang.name, true) then
      for _, move in ipairs(data) do
        if move.from == player then
          for _, info in ipairs(move.moveInfo) do
            if Fk:getCardById(info.cardId).color == Card.Black and
              (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) then
              return true
            end
          end
        end
      end
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    for _, move in ipairs(data) do
      if move.from == player and player:hasSkill(tuilang.name, true) then
        for _, info in ipairs(move.moveInfo) do
          if Fk:getCardById(info.cardId).color == Card.Black and
            (info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip) then
            room:addPlayerMark(player, "@lingling__tuilang", 1)
          end
        end
        while player:getMark("@lingling__tuilang") >= 3 do
          room:removePlayerMark(player, "@lingling__tuilang", 3)
          if player:getMark("@lingling__tuilang") == 0 then
            move.extra_data = move.extra_data or {}
            move.extra_data.lingling__tuilang = move.extra_data.lingling__tuilang or {}
            table.insertIfNeed(move.extra_data.lingling__tuilang, player.id)
          end
        end
      end
    end
  end,
})

tuilang:addLoseEffect(function (self, player, is_death)
  player.room:setPlayerMark(player, "@lingling__tuilang", 0)
end)

return tuilang
